﻿using System.Collections.Generic;
using System.Linq;
using Gola.VN.Scb.Web.Common.Entities;
using Gola.VN.Scb.Web.Common.Models;
using Gola.VN.Scb.Web.Dal;

namespace Gola.VN.Scb.Web.Bll
{
    internal class SearchHistoryBiz : BaseBiz
    {
        internal SearchHistoryBiz(MainDao mainDao)
            : base(mainDao)
        {
        }

        internal void InitializeData()
        {
            // 1. get data from database
            DaoPackage package = new DaoPackage();
            package.Channel = ChannelType.Sql;
            package.Event = DaoEvent.GetSearchHistories;
            _mainDao.Execute(package);

            // 2. update data into memory
            package.Channel = ChannelType.Memory;
            package.Event = DaoEvent.SetSearchHistories;
            package.Input = package.Output;
            _mainDao.Execute(package);
        }

        internal int UpdateSearchHistory(SearchHistoryEntity searchHistoryEntity)
        {
            // 2. update data into database
            DaoPackage package = new DaoPackage();
            package.Channel = ChannelType.Sql;
            package.Input = searchHistoryEntity;
            package.Event = DaoEvent.UpdateSearchHistory;
            _mainDao.Execute(package);

            // 2. update data into memory
            package.Channel = ChannelType.Memory;
            package.Event = DaoEvent.UpdateSearchHistory;
            _mainDao.Execute(package);

            return (int)package.Output;
        }

        internal List<SearchHistoryEntity> GetSearchHistories(string keyword)
        {
            // 2. update data into database
            DaoPackage package = new DaoPackage();
            package.Channel = ChannelType.Memory;
            package.Event = DaoEvent.GetSearchHistories;
            _mainDao.Execute(package);

            return package.Output as List<SearchHistoryEntity>;
        }

        internal List<UISearchResultModels> DoSearch(string searchText, int maxResults = 10)
        {
            DaoPackage package = new DaoPackage();
            //get all product
            package.Channel = ChannelType.Memory;
            package.Event = DaoEvent.GetProducts;
            _mainDao.Execute(package);
            var products = package.Output as IEnumerable<ProductEntity>;
            products = products ?? new List<ProductEntity>();
            //get all categories
            package.Event = DaoEvent.GetCategoriesAsFlat;
            _mainDao.Execute(package);
            var categories = package.Output as IEnumerable<CategoryEntity>;
            categories = categories ?? new List<CategoryEntity>();
            var result = products.Where(
              p => p.Title.StartsWith(searchText, System.StringComparison.CurrentCultureIgnoreCase)).Take(maxResults).
              Select(s => new UISearchResultModels { Name = s.Title, View = s.View }).ToList();
            result.AddRange(
                categories.Where(
                c => c.Name.StartsWith(searchText, System.StringComparison.CurrentCultureIgnoreCase)).Select(b => new UICategoryModels
                {
                    Id = b.Id,
                    Name = b.Name
                }).Take(maxResults).
                                        Select(s => new UISearchResultModels
                                        {
                                            Name = s.Name,
                                            View = s.InnerLink
                                        }).ToList());

            result = result.OrderBy(s => s.Name).Take(maxResults).ToList();

            return result;
        }
    }
}
